<?php

/**
 * 滑稽pro要求wordpress版本在4.1以上
 */
if ( version_compare( $GLOBALS['wp_version'], '4.1-alpha', '<' ) ) {
	require get_template_directory() . '/inc/back-compat.php';
}

/*
* 初始化滑稽pro
*/
if ( ! function_exists( 'huajipro_setup' ) ) :
function huajipro_setup() {
	/**
	* 文章特色图像大小
	*/
	$huaji_post_thumbnail_width=825;
	$huaji_post_thumbnail_Height=510;

	/*
	* i18n
	*/
	load_theme_textdomain( 'huaji-pro', get_template_directory() . '/languages' );

	/*
	* 支持自定义标题
	*/
	add_theme_support( 'title-tag' );

	/*
	* 支持文章特色图像
	*/
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( $huaji_post_thumbnail_width, $huaji_post_thumbnail_Height, true );

	/*
	* 支持的文章形式
	*/
	add_theme_support( 'post-formats', array(
		'aside', 'image', 'video', 'quote', 'link', 'gallery', 'status', 'audio', 'chat'
	) );

	/*
	* 支持站点logo
	*/
	add_theme_support( 'custom-logo', array(
		'height'      => 248,
		'width'       => 248,
		'flex-height' => true,
	) );

	/*
	* 背景颜色
	*/
	// add_theme_support( 'custom-background', apply_filters( 'twentyfifteen_custom_background_args', array(
	// 	'default-color'      => $default_color,
	// 	'default-attachment' => 'fixed',
	// ) ) );

	/*
	 * 编辑器样式
	 */
	add_editor_style( array( 'Bootstrap/css/bootstrap.min.css') );

	/*
	 * 小工具选择性刷新
	 */
	add_theme_support( 'customize-selective-refresh-widgets' );

	//注册菜单
	register_nav_menus( array(
		'top_left' => __( '顶部菜单(左侧)',      'huaji-pro' ),
		'top_right'  => __( '顶部菜单(右侧)', 'huaji-pro' ),
		'sidebar_top' => __('侧边栏(顶部)','huaji-pro'),
		'sidebar_bottom' => __('侧边栏(底部)','huaji-pro'),
		'footer-friend' => __('友情链接','huaji-pro')
	) );
}
endif;
add_action( 'after_setup_theme', 'huajipro_setup' );

/*
 * 注册小工具
 */
function huajipro_widgets_init() {
	register_sidebar( array(
		'name'          => __( '小工具区域', 'huaji-pro' ),
		'id'            => 'sidebar-1',
		'description'   => __( '向侧边栏添加小工具。', 'huaji-pro' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h5 class="widget-title"><strong>',
		'after_title'   => '</strong></h5>',
	) );

	// register_sidebar( array(
	// 	'name'          => __( '底部小工具区域', 'huaji-pro' ),
	// 	'id'            => 'footer-1',
	// 	'description'   => __( '向底部添加小工具。', 'huaji-pro' ),
	// 	'before_widget' => '<aside id="%1$s" class="widget %2$s">',
	// 	'after_widget'  => '</aside>',
	// 	'before_title'  => '<h5 class="widget-title"><strong>',
	// 	'after_title'   => '</strong></h5>',
	// ) );
}
add_action( 'widgets_init', 'huajipro_widgets_init' );

// if ( ! function_exists( 'huaji_entry_meta' ) ) :
// function huaji_entry_meta() {
// 	if ( is_sticky() && is_home() && ! is_paged() ) {
// 		printf( '<span class="sticky-post">%s</span>', __( 'Featured', 'twentyfifteen' ) );
// 	}

// 	$format = get_post_format();
// 	if ( current_theme_supports( 'post-formats', $format ) ) {
// 		printf( '<span class="entry-format">%1$s<a href="%2$s">%3$s</a></span>',
// 			sprintf( '<span class="screen-reader-text">%s </span>', _x( 'Format', 'Used before post format.', 'twentyfifteen' ) ),
// 			esc_url( get_post_format_link( $format ) ),
// 			get_post_format_string( $format )
// 		);
// 	}

// 	if ( in_array( get_post_type(), array( 'post', 'attachment' ) ) ) {
// 		$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';

// 		if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
// 			$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
// 		}

// 		$time_string = sprintf( $time_string,
// 			esc_attr( get_the_date( 'c' ) ),
// 			get_the_date(),
// 			esc_attr( get_the_modified_date( 'c' ) ),
// 			get_the_modified_date()
// 		);

// 		printf( '<span class="posted-on"><span class="screen-reader-text">%1$s </span><a href="%2$s" rel="bookmark">%3$s</a></span>',
// 			_x( 'Posted on', 'Used before publish date.', 'twentyfifteen' ),
// 			esc_url( get_permalink() ),
// 			$time_string
// 		);
// 	}

// 	if ( 'post' == get_post_type() ) {
// 		if ( is_singular() || is_multi_author() ) {
// 			printf( '<span class="byline"><span class="author vcard"><span class="screen-reader-text">%1$s </span><a class="url fn n" href="%2$s">%3$s</a></span></span>',
// 				_x( 'Author', 'Used before post author name.', 'twentyfifteen' ),
// 				esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
// 				get_the_author()
// 			);
// 		}

// 		$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfifteen' ) );
// 		if ( $categories_list && twentyfifteen_categorized_blog() ) {
// 			printf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
// 				_x( 'Categories', 'Used before category names.', 'twentyfifteen' ),
// 				$categories_list
// 			);
// 		}

// 		$tags_list = get_the_tag_list( '', _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfifteen' ) );
// 		if ( $tags_list ) {
// 			printf( '<span class="tags-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
// 				_x( 'Tags', 'Used before tag names.', 'twentyfifteen' ),
// 				$tags_list
// 			);
// 		}
// 	}

// 	if ( is_attachment() && wp_attachment_is_image() ) {
// 		// Retrieve attachment metadata.
// 		$metadata = wp_get_attachment_metadata();

// 		printf( '<span class="full-size-link"><span class="screen-reader-text">%1$s </span><a href="%2$s">%3$s &times; %4$s</a></span>',
// 			_x( 'Full size', 'Used before full size attachment link.', 'twentyfifteen' ),
// 			esc_url( wp_get_attachment_url() ),
// 			$metadata['width'],
// 			$metadata['height']
// 		);
// 	}

// 	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
// 		echo '<span class="comments-link">';
// 		/* translators: %s: post title */
// 		comments_popup_link( sprintf( __( 'Leave a comment<span class="screen-reader-text"> on %s</span>', 'twentyfifteen' ), get_the_title() ) );
// 		echo '</span>';
// 	}
// }
// endif;

function huajipro_strimwidth($str ,$start , $width ,$trimmarker ){
    $output = preg_replace('/^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start.'}((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$width.'}).*/s','\1',$str);
    return $output.$trimmarker;
}

function timeago( $ptime ) {
    $ptime = strtotime($ptime);
    $etime = time() - $ptime;
    if($etime < 1) return '刚刚';
    $interval = array (
        12 * 30 * 24 * 60 * 60  =>  '年前 ('.date('Y-m-d', $ptime).')',
        30 * 24 * 60 * 60       =>  '个月前 ('.date('m-d', $ptime).')',
        7 * 24 * 60 * 60        =>  '周前 ('.date('m-d', $ptime).')',
        24 * 60 * 60            =>  '天前',
        60 * 60                 =>  '小时前',
        60                      =>  '分钟前',
        1                       =>  '秒前'
    );
    foreach ($interval as $secs => $str) {
        $d = $etime / $secs;
        if ($d >= 1) {
            $r = round($d);
            return $r . $str;
        }
    };
}

//输出缩略图地址
function post_thumbnail_src(){
    global $post;
	if( $values = get_post_custom_values("thumb") ) {	//输出自定义域图片地址
		$values = get_post_custom_values("thumb");
		$post_thumbnail_src = $values [0];
	} elseif( has_post_thumbnail() ){    //如果有特色缩略图，则输出缩略图地址
        $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
		$post_thumbnail_src = $thumbnail_src [0];
    } else {
		$post_thumbnail_src = '';
		ob_start();
		ob_end_clean();
		$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
		$post_thumbnail_src = $matches [1] [0];   //获取该图片 src
		if(empty($post_thumbnail_src)){	//如果日志中没有图片，则显示随机图片
			$random = mt_rand(1, 10);
			echo get_bloginfo('template_url');
			echo '/img/pic/'.$random.'.jpg';
			//如果日志中没有图片，则显示默认图片
			//echo '/img/thumbnail.png';
		}
	};
	echo $post_thumbnail_src;
}

function deel_views($after=''){
  global $post;
  $post_ID = $post->ID;
  $views = (int)get_post_meta($post_ID, 'views', true);
  echo $views, $after;
}

function huajipro_scripts_styles(){
	wp_enqueue_style( 'huaji-pro-style', get_stylesheet_uri() );

	//ie9以下支持
	wp_enqueue_style( 'html5shiv', 'http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js',array(),'3.7.2' );
	wp_style_add_data( 'html5shiv', 'conditional', 'lt IE 9' );
	wp_enqueue_style( 'respond', 'http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js',array(),'1.4.2' );
	wp_style_add_data( 'respond', 'conditional', 'lt IE 9' );

	wp_enqueue_script('theme-script',get_template_directory_uri() . '/dist/js/all.js',array('jquery'),'0.0.1',true);
}
add_action( 'wp_enqueue_scripts', 'huajipro_scripts_styles' );

?>